﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGinnBPM.Lib.Interfaces;
using System.Collections;
using NLog;

namespace NGinnBPM.Lib.Util
{
    /// <summary>
    /// Config provider based on name-value dictionary
    /// </summary>
    public class DictionaryConfigProvider : IConfigParameterProvider
    {
        private Dictionary<string, string> _dic = new Dictionary<string,string>();
        private static Logger log = LogManager.GetCurrentClassLogger();

        public DictionaryConfigProvider(IDictionary options)
        {
            foreach(string k in options.Keys)
                _dic[k] = Convert.ToString(options[k]);
        }

        public DictionaryConfigProvider()
        { 
        }

        public void CopyFrom(IConfigParameterProvider cfg)
        {
            foreach (string prm in cfg.GetParameterNames())
            {
                Set(prm, cfg.GetString(prm));
            }
        }

        public void Set(string name, string value)
        {
            if (_dic.ContainsKey(name))
                _dic.Remove(name);
            _dic[name] = value;
        }

        #region IConfigParameterProvider Members

        public string GetString(string paramName)
        {
            string s;
            if (!_dic.TryGetValue(paramName, out s))
            {
                log.Info("Parameter not set: {0}", paramName);
                return null;
            }
            return s;
        }

        public string GetString(string paramName, string defaultValue)
        {
            string s = GetString(paramName);
            return s == null ? defaultValue : s;
        }

        public bool GetBool(string paramName, bool defVal)
        {
            string s = GetString(paramName);
            if (s == null) return defVal;
            s = s.ToUpper();
            return s == "1" || s == "TRUE" || s == "YES";
        }

        public int GetInt(string paramName, int defaultValue)
        {
            string s = GetString(paramName);
            return s == null || s.Length == 0 ? defaultValue : Convert.ToInt32(s);
        }

        public double GetDouble(string paramName, double defaultValue)
        {
            string s = GetString(paramName);
            return s == null || s.Length == 0 ? defaultValue : Convert.ToDouble(s);
        }

        #endregion

        #region IConfigParameterProvider Members


        public IList<string> GetParameterNames()
        {
            return new List<string>(_dic.Keys);
        }

        #endregion

        #region IConfigParameterProvider Members


        public void Set(string param, int val)
        {
            Set(param, val.ToString());
        }

        public void Set(string param, double val)
        {
            Set(param, val.ToString());
        }

        public void Set(string param, bool val)
        {
            Set(param, val.ToString());
        }

        #endregion
    }
}
